package org.dfl.plugin.editor.parts;

import org.dfl.plugin.editor.figures.FiguresConstants;
import org.dfl.plugin.editor.figures.InputPlaceFigure;
import org.dfl.plugin.editor.figures.OutputPlaceFigure;
import org.dfl.plugin.editor.figures.PlaceFigure;
import org.dfl.plugin.editor.model.InputPlaceModelElement;
import org.dfl.plugin.editor.model.OutputPlaceModelElement;
import org.dfl.plugin.editor.model.PlaceModelElement;
import org.eclipse.draw2d.IFigure;

/**
 * Edit part used for places
 * @author Piotr Wlodarczyk
 *
 */
public class PlaceEditPart extends ShapeEditPart {
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		PlaceModelElement place = getCastedModel();
		PlaceFigure f = null;
		
		if(place instanceof InputPlaceModelElement){
			f = new InputPlaceFigure(place.getLabelText(),place.getTipText());
		}else if(place instanceof OutputPlaceModelElement){
			f = new OutputPlaceFigure(place.getLabelText(),place.getTipText());
		}else{
			f = new PlaceFigure(place.getLabelText(),place.getTipText());	
		}
		
		f.setOpaque(true); // non-transparent figure
		refreshFigure(f);
		return f;
	}	

	public PlaceModelElement getCastedModel() {
		return (PlaceModelElement) getModel();
	}

	@Override
	protected void refreshFigure(IFigure ff) {
		PlaceFigure f = (PlaceFigure) ff;
		f.setLabelText(getCastedModel().getLabelText());
		f.setTipText(getCastedModel().getTipText());

		// ustawianie koloru w zaleznosci od tego, czy sa w srodku dane, czy nie ma
		if(!getCastedModel().isConnected() && !getCastedModel().isNotEmptyData()){
			f.setBackgroundColor(FiguresConstants.placeColorNotDefined);
		}else{
			if(getCastedModel() instanceof InputPlaceModelElement){
				f.setBackgroundColor(getCastedModel().isNotEmptyData()?FiguresConstants.inputPlaceColorData:FiguresConstants.inputPlaceColorNoData);
			}else if(getCastedModel() instanceof OutputPlaceModelElement){
				f.setBackgroundColor(getCastedModel().isNotEmptyData()?FiguresConstants.outputPlaceColorData:FiguresConstants.outputPlaceColorNoData);
			}else{
				f.setBackgroundColor(getCastedModel().isNotEmptyData()?FiguresConstants.placeColorData:FiguresConstants.placeColorNoData);
			}
		}
	}
}